﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using Twitterizer;

public static class HttpClientOld
{
    /// <summary>
    /// Ограничение на число запросов, текущий статтус
    /// </summary>
    public static RateLimiting Limit = new RateLimiting();

    private static void ExtractLimits(WebResponse response)
    {
        if (response == null) return;
        if (response.Headers.AllKeys.Contains("X-Ratelimit-Limit") &&
            !string.IsNullOrEmpty(response.Headers["X-Ratelimit-Limit"]))
        {
            Limit.Total = int.Parse(response.Headers["X-Ratelimit-Limit"], CultureInfo.InvariantCulture);
        }

        if (response.Headers.AllKeys.Contains("X-Ratelimit-Remaining") &&
            !string.IsNullOrEmpty(response.Headers["X-Ratelimit-Remaining"]))
        {
            Limit.Remaining = int.Parse(response.Headers["X-Ratelimit-Remaining"], CultureInfo.InvariantCulture);
        }
        Limit.ResetDate = DateTime.UtcNow; //пускай будет последний удачный запрос
    }

    public static string GET(string uri)
    {
        try
        {
            var request = HttpWebRequest.Create(uri);
            request.Method = "GET";
            var response = request.GetResponse();
            ExtractLimits(response);
            return ReadResponse(response.GetResponseStream());
        }
        catch (WebException ex)
        {
            ExtractLimits(ex.Response);
            if (ex.Response != null)
            {
                return ReadResponse(ex.Response.GetResponseStream());
            }
            return String.Empty;
        }
    }

    public static string POST(string url)
    {
        return POST(url, new Dictionary<string, string>());
    }

    public static string POST(string url, IDictionary<string, string> args)
    {
        try
        {
            var parameters = new NameValueCollection();
            foreach (var arg in args)
            {
                parameters.Add(arg.Key, arg.Value);
            }

            var client = new WebClient();

            var result = client.UploadValues(url, "POST", parameters);

            return Encoding.Default.GetString(result);
        }
        catch (WebException ex)
        {
            return ReadResponse(ex.Response.GetResponseStream());
        }
    }

    public static string DELETE(string uri)
    {
        var request = HttpWebRequest.Create(uri);
        request.Method = "DELETE";

        return DELETE(uri, new Dictionary<string, string>());

        // return ReadResponse(request.GetResponse().GetResponseStream());
    }

    public static string DELETE(string uri, IDictionary<string, string> args)
    {
        try
        {
            var parameters = new NameValueCollection();
            foreach (var arg in args)
            {
                parameters.Add(arg.Key, arg.Value);
            }

            var client = new WebClient();

            var result = client.UploadValues(uri, "DELETE", parameters);

            return Encoding.Default.GetString(result);
        }
        catch (WebException ex)
        {
            return ReadResponse(ex.Response.GetResponseStream());
        }
    }

    private static string ReadResponse(Stream response)
    {
        StreamReader reader = new StreamReader(response);
        string line;
        StringBuilder result = new StringBuilder();
        while ((line = reader.ReadLine()) != null)
        {
            result.Append(line);
        }
        return result.ToString();
    }
}
